package mobi.hami.huapin.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import mobi.hami.huapin.base.Result;
import mobi.hami.huapin.constants.BaseEnums;
import mobi.hami.huapin.dto.PageDTO;
import mobi.hami.huapin.model.Product;
import mobi.hami.huapin.service.ProductService;
import mobi.hami.huapin.util.Results;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;

@RestController
@RequestMapping(value = "/shot", produces = "application/json")
@Api(description = "商品表api")
public class ProductController {

    @Autowired
    private ProductService productService;

    @RequestMapping(value = "/productAdd.do", method = RequestMethod.POST)
    @ApiOperation(value = "保存商品分类", notes = " ")
    public Result productAdd(@RequestBody Product product) {
        product.setProduct_code((product.getName() + product.getId()).hashCode());
        product.setDeleted(0);
        int rs = productService.save(product);
        return Results.successWithData(rs, BaseEnums.SUCCESS.code(), BaseEnums.SUCCESS.desc());
    }

    @RequestMapping(value = "/update.do", method = RequestMethod.POST)
    @ApiOperation(value = "保存商品信息（spu）", notes = " ")
    public Result update(@RequestBody Product product) {
        int rs = productService.update(product);

        return Results.successWithData(rs, BaseEnums.SUCCESS.code(), BaseEnums.SUCCESS.desc());
    }

    @RequestMapping(value = "/list.do", method = RequestMethod.POST)
    @ApiOperation(value = "获取spu", notes = " ")
    public Result getList(@RequestBody PageDTO page)
    {
        page.setData(new ArrayList<Product>());
        page.setPageindex((page.getPageindex() - 1) * page.getPagesize() >= 0 ? (page.getPageindex() - 1) * page.getPagesize() : 0);
        page.setPagesize(page.getPagesize() > 0 ? page.getPagesize() : Integer.MAX_VALUE);
        PageDTO<Product> products = productService.getListPage(page);
        return Results.successWithData(products, BaseEnums.SUCCESS.code(), BaseEnums.SUCCESS.desc());
    }
    @RequestMapping(value = "/updateDelete", method = RequestMethod.POST)
    @ApiOperation(value = "删除商品", notes = " ")
    public Result productDelete(@RequestBody Product product) {
        return Results.successWithData(productService.updateDelete(product), BaseEnums.SUCCESS.code(), BaseEnums.SUCCESS.desc());
    }
}
